package com.gofunbuy.app.member;

import java.util.HashMap;

import com.gofunbuy.MainActivity;
import com.gofunbuy.R;
import com.gofunbuy.TabMemberGroup;
import com.gofunbuy.TabOthersGroup;
import com.gofunbuy.app.home.HomeActivity;
import com.gofunbuy.app.others.TabOthersActivity;
import com.gofunbuy.http.HttpClientImpl;
import com.gofunbuy.util.GoFunBuyUtils;
import com.gofunbuy.util.GoFunBuyUtils.GoFunBuyInterface;
import com.gofunbuy.util.GoFunBuyUtils.GoFunBuyInterface.ProtocalStatics;
import com.gofunbuy.util.GoFunBuyUtils.IntegrationXML;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class SubMemberIntegrationActivity extends Activity implements
		OnClickListener, GoFunBuyInterface {
	private TextView tvBack, tvTitle;
	private TextView tvIntegraionLookDetail, tvIntegraionShopping,
			tvDonationLookDetail, tvDonationShopping;
	private TextView tvDonation, tvIntegration;
	private Context mContext;
	private IntegrationXML mIntegrationMessage;
	private int backToWhich = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.member_integration_layout);
		mContext = this;
		backToWhich = getIntent().getIntExtra(BACK_TO_WHO, -1);
		initLayout();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (!GoFunBuyUtils.isNetworkConnected(mContext)) {
					mHandler.sendEmptyMessage(MSG_NETWORD_ERROR);
					return;
				}
				HashMap<String, String> params = new HashMap<String, String>();
				try {
					params.put(ProtocalStatics.SHOPCODE, "goinside");
					params.put(ProtocalStatics.USERID, GoFunBuyUtils.mUserID);
					mIntegrationMessage = HttpClientImpl.getInstance(mContext)
							.getIntegrationAndDonation(mHandler, params);
					if (mIntegrationMessage == null) {
						mHandler.sendMessage(mHandler.obtainMessage(
								MSG_GET_MESSAGE_STATE, -1, 0));
					} else {
						mHandler.sendMessage(mHandler.obtainMessage(
								MSG_GET_MESSAGE_STATE, 1, 0));
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}).start();
	}

	private void initLayout() {

		tvTitle = (TextView) findViewById(R.id.topbar_title);
		tvTitle.setText(R.string.title_ingegration);
		tvBack = (TextView) findViewById(R.id.topbar_back);
		tvBack.setOnClickListener(this);

		tvIntegraionLookDetail = (TextView) findViewById(R.id.integration_look);
		tvIntegraionLookDetail.setOnClickListener(this);
		tvIntegraionShopping = (TextView) findViewById(R.id.integration_buy);
		tvIntegraionShopping.setOnClickListener(this);

		tvDonationLookDetail = (TextView) findViewById(R.id.donation_look);
		tvDonationLookDetail.setOnClickListener(this);

		tvDonationShopping = (TextView) findViewById(R.id.donation_buy);
		tvDonationShopping.setOnClickListener(this);

		tvDonation = (TextView) findViewById(R.id.id_member_donation);
		tvIntegration = (TextView) findViewById(R.id.id_member_integration);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.topbar_back:
			if (backToWhich == 5)
				startOthersActivity();
			else
				startActivity(SubMemberDocumentActivity.class);
			break;
		case R.id.integration_look:
			startActivity(SubMemberPayHistoryActivity.class);
			break;
		case R.id.integration_buy:
			startExactTabActivity(TAB_SHOPING);
			break;
		case R.id.donation_look:
			startActivity(SubMemberPayHistoryActivity.class);
			break;
		case R.id.donation_buy:
			startExactTabActivity(TAB_SHOPING);
			break;
		}
	}

	private void startActivity(Class<?> a) {
		// 要跳转的Activity
		// Log.v("名字", a.getSimpleName() + " " + a.getName());
		Intent intent = new Intent(SubMemberIntegrationActivity.this, a);
		// 把Activity转换成一个Window，然后转换成View
		Window w = TabMemberGroup.group.getLocalActivityManager()
				.startActivity(a.getSimpleName(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		TabMemberGroup.group.setContentView(view);
	}

	private void startOthersActivity() {
		// 要跳转的Activity
		// Log.v("名字", a.getSimpleName() + " " + a.getName());
		Intent intent = new Intent(this, TabOthersActivity.class);
		// 把Activity转换成一个Window，然后转换成View
		Window w = TabOthersGroup.group.getLocalActivityManager()
				.startActivity(TabOthersActivity.class.getSimpleName(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		TabOthersGroup.group.setContentView(view);
	}

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case MSG_NETWORD_ERROR:
				Toast.makeText(mContext, R.string.network_error,
						Toast.LENGTH_SHORT).show();
				break;
			case MSG_GET_MESSAGE_STATE:
				if (msg.arg1 == -1)
					Toast.makeText(mContext, R.string.donate_message_fail,
							Toast.LENGTH_SHORT).show();
				else if (msg.arg1 == 1) {
					tvDonation.setText(mIntegrationMessage.donation);
					tvIntegration.setText(mIntegrationMessage.jp);
				}
				break;
			}
		}

	};

	private void startExactTabActivity(int pose) {
		Intent it = new Intent(this, MainActivity.class);
		it.putExtra(TAB_POSE, pose);
		if (pose != 0)
			it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(it);
		finish();
	}
}
